import sys
import os

dirname = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(dirname)

from utils.ids import create_id
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy import Column, String, BigInteger, CHAR, DateTime
from utils.database import Base, engine
from datetime import datetime


class PMIFailure(Base):
    __tablename__ = 'tb_pmi_failure'
    __table_args__ = {'comment': 'PMI缺陷表', 'mysql_charset': 'utf8mb4'}

    id = Column(BigInteger, primary_key=True,
                   autoincrement=True, comment='id')
    failure_id = Column(
        BigInteger, nullable=False, comment='缺陷 id 做关联查询')
    del_flag = Column(
        CHAR(1), nullable=False, server_default='0', comment="是否删除, 1-已删除, 0-未删除")


class PMIDevice(Base):
    __tablename__ = 'tb_pmi_device'
    __table_args__ = {'comment': 'pmi 设备表', 'mysql_charset': 'utf8mb4'}

    id = Column(BigInteger, primary_key=True,
                   autoincrement=True, comment='id')
    device_code = Column(
        String(32), nullable=False, default=create_id, comment='device code')
    device_id = Column(
        String(32), nullable=False, default=create_id, comment='device id')
    del_flag = Column(
        CHAR(1), nullable=False, server_default='0', comment="是否删除, 1-已删除, 0-未删除")


class PMILot(Base):
    __tablename__ = 'tb_pmi_lot'
    __table_args__ = {'comment': 'pmi lot 表', 'mysql_charset': 'utf8mb4'}

    id = Column(BigInteger, primary_key=True,
                   autoincrement=True, comment='id')
    lot_code = Column(
        String(32), nullable=False, default=create_id, comment='lot code')
    device_code = Column(
        String(32), nullable=False, comment='device code 做关联')
    lot_name = Column(
        String(32), nullable=False, comment='lot名称')
    dir_path = Column(
        String(255), nullable=False, server_default='', comment='lot文件夹目录路径')
    status = Column(
        String(16), nullable=False, server_default='NO_COMPLETED', comment='状态: NO_COMPLETED-未完成, COMPLETED-已完成')
    create_time = Column(DateTime, default=datetime.utcnow, comment='创建时间')
    del_flag = Column(
        CHAR(1), nullable=False, server_default='0', comment="是否删除, 1-已删除, 0-未删除")
    

class PMIWafer(Base):
    __tablename__ = 'tb_pmi_wafer'
    __table_args__ = {'comment': 'pmi wafer 表', 'mysql_charset': 'utf8mb4'}

    id = Column(BigInteger, primary_key=True,
                   autoincrement=True, comment='id')
    wafer_code = Column(
        String(32), nullable=False, default=create_id, comment='wafer code')
    lot_code = Column(
        String(32), nullable=False, comment='lot code 做关联查询')
    wafer_id = Column(
        String(32), nullable=False, comment='wafer 编号')
    del_flag = Column(
        CHAR(1), nullable=False, server_default='0', comment="是否删除, 1-已删除, 0-未删除")


class PMIImage(Base):
    __tablename__ = 'tb_pmi_image'
    __table_args__ = {'comment': 'image 表', 'mysql_charset': 'utf8mb4'}

    id = Column(BigInteger, primary_key=True,
                   autoincrement=True, comment='id')
    wafer_code = Column(
        String(32), nullable=False, comment='wafer code 做关联')
    file_path = Column(String(255), nullable=False, comment='文件服务器径')
    file_name = Column(String(32), nullable=False, comment='文件名')
    detection_info = Column(
        LONGTEXT, comment='检测信息')
    segmentation_info = Column(
        LONGTEXT, comment='分割信息')
    failure_id = Column(
        BigInteger, nullable=False, comment='缺陷 id 做关联查询')
    del_flag = Column(
        CHAR(1), nullable=False, server_default='0', comment="是否删除, 1-已删除, 0-未删除")
    

class PMIProduct(Base):
    __tablename__ = 'tb_pmi_product'
    __table_args__ = {'comment': '产品型号表', 'mysql_charset': 'utf8mb4'}

    id = Column(BigInteger, primary_key=True,
                   autoincrement=True, comment='id')
    product_code = Column(
        String(32), nullable=False, default=create_id, comment='product code')
    product_name = Column(
        String(32), nullable=False, comment='product name')
    parameter_info = Column(
        LONGTEXT, nullable=False, comment='参数信息')
    del_flag = Column(
        CHAR(1), nullable=False, server_default='0', comment="是否删除, 1-已删除, 0-未删除")


if __name__ == "__main__":
    # 创建表
    Base.metadata.create_all(bind=engine)

